<extend name="Public/base"/>
<block name="style">
    <link rel="stylesheet" type="text/css" href="__CSS__/tabStyle.css" media="all">
    <script type="text/javascript" src="__JS__/tabScript.js"></script>
</block>
<block name="body">
	<div class="main-title">
		<h2>代客下单</h2>
	</div>
	<div class="tools auth-botton">
        <button id="addcartbyall" class="btn">加入购物车</button>
        <a id="cartbtn" class="btn" href="{:U('cart')}">购物车({$totalnum})</a>
        <button id="cleancart" class="btn">清空</button>
	</div>
    <ul class="tabs">
        <volist name="typelist" id="type">
            <li><a href="#" name=".tab1_{$i}">{$type.name}</a></li>
        </volist>     
    </ul>
    <div id="tablediv" class="content">
        <volist name="typelist" id="type" key="k">
            <div class="tab1_{$k} data-table table-striped">
                <table class="" id="dtable">
                    <thead>
                        <tr>
                            <th>名称</th>
                            <th>规格</th>
                            <th>单位</th>
                            <th>单价</th>
                            <th>数量</th>
                            <th>合计</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <notempty name="type['goodslist']">
                        <volist name="type['goodslist']" id="vo">
                            <tr>
                                <td>
                                    <label id="goodsname">{$vo.name}</label>
                                    <input type="hidden" name="goodsid" id="goodsid" value="{$vo.id}">
                                    <input type="hidden" name="typeid" id="typeid" value="{$vo.type_id}">
                                </td>
                                <td>
                                    <select id="spe" name="spe">
                                        <volist name="vo.spelist" id="sl">
                                            <option value="{$sl.id}">{$sl.name}</option>
                                        </volist>
                                    </select>
                                </td>
                                <td><label id="unit">{$vo['spelist'][0]['unit']}</label></td>
                                <td><input type="text" id="price" name="price" class="price" value="{$vo['spelist'][0]['price']}"></td>
                                <td>
                                    <input type="text" id="num" name="num" class="num" value="{$num}">
                                    <!-- <label id="goodsname">已下单:{$vo.name}</label> -->
                                </td>
                                <td><input type="text" class="text" name="total" id="total" readonly="readonly"></td>
                                <td><a href="#" id="addcart" class="addcart">添加</a></td>
                            </tr>
                        </volist>
                        <else/>
                        <td colspan="7" class="text-center">暂时还没有数据！</td>
                        </notempty>
                    </tbody>
                </table>
            </div>
        </volist>
    </div>
</block>
<block name="script">
    <script type="text/javascript">
    	$(function(){
            loadTab();

            var selall = $(this).find("select");
            // $("select").each(function(){
            //     var speid = $(this).parents("tr").find("#spe").val();
            //     var sel = $(this);
            //     $.get("{:U('Order/getGoodsSpe')}",{speid:speid},function(data){
            //         if(data != null){
            //             sel.parents("tr").find("#unit").html(data['unit']);
            //             sel.parents("tr").find("#price").val(data['price']);
            //         }
            //     })
            // });

            $("select",this).change(function(){
            	var speid = $(this).parents("tr").find("#spe").val();
            	var sel = $(this);
            	$.get("{:U('Order/getGoodsSpe')}",{speid:speid},function(data){
                    if(data != null){
                		sel.parents("tr").find("#unit").html(data['unit']);
                		sel.parents("tr").find("#price").val(data['price']);
                    }
            	})
            });

            $(".price",this).blur(function(){
        		var price = $(this).parents("tr").find("#price").val();
        		var num = $(this).parents("tr").find("#num").val();
        		if(price != "")
        		{
        			var total = totalprice(price,num);
                	$(this).parents("tr").find("#total").val(total);
        		}else{
                    $(this).parents("tr").find("#price").val(0);
                    var total = totalprice(0,num);
                    $(this).parents("tr").find("#total").val(total);
                }
            });

            $(".num",this).blur(function(){
                var price = $(this).parents("tr").find("#price").val();
        		var num = $(this).parents("tr").find("#num").val();
                if(num != "")
        		{
        			var total = totalprice(price,num);
                	$(this).parents("tr").find("#total").val(total);
        		}else{
                    $(this).parents("tr").find("#total").val("");
                }
            });

            $('#cleancart').click(function(){
            	$.get("{:U('Order/cleanCart')}",{},function(data){
                    $('#cartbtn').text('购物车(0)');
            		updateAlert("购物车已清空！",'alert-success');
                    setTimeout(function(){
                        $("#top-alert").removeClass("block");
                    },1500);
            	})
            });

            $('.addcart').click(function(){
            	var sel = $(this);
            	var goodsid = $(this).parents("tr").find("#goodsid").val();
                var typeid = $(this).parents("tr").find("#typeid").val();
            	var speid = $(this).parents("tr").find("#spe").val();
            	var price = $(this).parents("tr").find("#price").val();
        		var num = $(this).parents("tr").find("#num").val();
        		var unit = $(this).parents("tr").find("#unit").html();
        		var goodsname = $(this).parents("tr").find("#goodsname").html();
            	$.post("{:U('Order/addCart')}",{speid:speid,price:price,num:num,unit:unit,goodsname:goodsname,typeid:typeid,goodsid:goodsid},function(data){
            		var obj = JSON.parse(data);
            		if(obj.state_code == 0){
            			$('#cartbtn').text('购物车('+obj.state_message+')');
            		}else{
                        updateAlert(obj.state_message);
                        setTimeout(function(){
                            $("#top-alert").removeClass("block");
                        },1500);
            		}
            	})
            });

            $('#addcartbyall').click(function(){
                $("tbody tr").each(function(){
                    var num = $(this).find("#num").val();
                    if(typeof(num) != "undefined" && num != "" && num != 0){
                        var goodsid = $(this).find("#goodsid").val();
                        var typeid = $(this).find("#typeid").val();
                        var speid = $(this).find("#spe").val();
                        var price = $(this).find("#price").val();
                        var num = $(this).find("#num").val();
                        var unit = $(this).find("#unit").html();
                        var goodsname = $(this).find("#goodsname").html();
                        $.post("{:U('Order/addCart')}",{speid:speid,price:price,num:num,unit:unit,goodsname:goodsname,typeid:typeid,goodsid:goodsid},function(data){
                            var obj = JSON.parse(data);
                            if(obj.state_code == 0){
                                $('#cartbtn').text('购物车('+obj.state_message+')');
                            }else{
                                updateAlert(obj.state_message);
                                setTimeout(function(){
                                    $("#top-alert").removeClass("block");
                                },1500);
                            }
                        })
                    }
                });
            });

        });
    	//合计金额
        function totalprice(price,num){
            var totalvar = price * num;
            return totalvar.toFixed(2);
        };
        //导航高亮
        highlight_subnav('{:U('Order/placeorder')}');
    </script>
</block>